﻿using Estsh.Core.Controllers;
using Estsh.Core.IServices;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Services;
using Estsh.Core.Services.IServices;
using Estsh.Core.Util;
using Estsh.Core.Wms.IServices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Text.Json;

namespace Estsh.Core.Web.Plugin.Wms.Controllers
{
    /// <summary>
    /// MoveOrderController
    /// </summary>
    [Route("wms/[controller]")]
    [ApiController]
    public class MoveOrderController : WmsBaseController
    {
        private IMoveOrderService service;
        private readonly IEnumService enumService;

        public MoveOrderController(IMoveOrderService _service, IEnumService _enumService)
        {
            this.service = _service;
            this.enumService = _enumService;
        }

        /// <summary>
        /// 获得单据类型列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetOrderTypeList")]
        public IActionResult GetOrderTypeList()
        {
	
			WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<SysEnum> data = enumService.getEnumList("wms_move_order_create_type");
                result.Success = true;
                result.Data = data;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            return Json(result);
        }


        /// <summary>
        /// 获得创建中的单据列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetCreateingList")]
        public IActionResult GetCreateingList()
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<WmsMoveHeader> data = service.GetCreateingList();
                result.Success = true;
                result.Data = data;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 单据明细列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetDetailList")]
        public IActionResult GetDetailList(JObject requestInfo)
        {
            string orderNo = requestInfo["orderNo"].ToString();
            WmsResponseResult_Extend result = new WmsResponseResult_Extend();
            try
            {
                List<WmsMoveDetail> data = service.GetMoveDetailList(orderNo);
                result.Success = true;
                result.Data = data;
                //result.ResRelevanceOrder=
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            return Json(result);
        }
        /// <summary>
        /// 处理扫描的箱条码
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="orderNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        [HttpPost("DoCarton")]
        public IActionResult DoCarton(JObject requestInfo)
        {
            string cartonNo = requestInfo["cartonNo"].ToString();
            string orderNo = requestInfo["orderNo"].ToString();
            int orderType = int.Parse(requestInfo["orderType"].ToString());
            string loginId = requestInfo["loginId"].ToString();
            string relevanceOrder = requestInfo["relevanceOrder"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                result = service.DoCarton(cartonNo, orderNo, orderType, loginId, relevanceOrder);
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 提交单据
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        [HttpPost("SubmitOrder")]
        public IActionResult SubmitOrder(JObject requestInfo)
        {
            string orderNo = requestInfo["orderNo"].ToString();
            string loginId = requestInfo["loginId"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                result = service.SubmitOrder(orderNo, loginId);
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }





    }
}
